﻿Imports CoreCode
Imports CoreCode.Connection

Public Class Sec_User
    Private clsDBCon As New Connection
    Private sSQLAdapter(8) As System.Data.SqlClient.SqlDataAdapter
    Private sDataSet As New DataSet

    Private WithEvents sBindSource As New BindingSource
    Private bsChild(7) As BindingSource

    Private lBinded As Boolean
    Private drv As DataRowView

    Private dtgName As String
    Private nBookMark As Single
    Private vModBookMark As Single

    Private vCurrentCOID As Integer = 0
    Private vCurrentRole As Integer = 0

    Public eAct As eAction

    Private Sub Sec_Role_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load      
        For i As Integer = 0 To 8
            sSQLAdapter(i) = New System.Data.SqlClient.SqlDataAdapter
        Next

        For i As Integer = 0 To 7
            bsChild(i) = New BindingSource
        Next
        nBookMark = 0
    End Sub

    Public Function TBConnect() As Boolean
        With clsDBCon
            .DatabaseName = .SecDatabaseName
            .DatabasePassword = .ConnectionPassword
            .DatabaseServer = .ConnectionServer
            .DatabaseServerType = ServerType.MSSQL
            .DatabaseUserName = .ConnectionUserName
            .Connect(.SQLCn)
            .DatabaseName = .SetDatabaseName
            .Connect(.SQLCnSet)
            .DatabaseName = .TranDatabaseName
            .Connect(.SQLCnTran)
            .DatabaseName = .SecDatabaseName
            Return .Connect(.SQLCnSec)
        End With
    End Function
    Public Function TBNew(ByVal obj As Object) As Boolean
        obj.tag = "newedit"
        eAct = eAction.NewRec
        TBNew = False
        Try
            With clsDBCon
                TBConnect()

                sDataSet.Tables.Clear()
                .DataUnbind(Me)
                .ObjectControl(Me, ObjectMethod.Cleardata)
                .ObjectControl(Me, ObjectMethod.ObjectLock, False)

                .OpenRecord(.SQLCnSec, sSQLAdapter(1), sDataSet, "*", sec_user_preference.Name)
                .OpenRecord(.SQLCnSec, sSQLAdapter(2), sDataSet, "*", sec_user_favorite.Name)
                .OpenRecord(.SQLCnSec, sSQLAdapter(3), sDataSet, "*", sec_user_company.Name)
                .OpenRecord(.SQLCnSec, sSQLAdapter(4), sDataSet, "*", sec_user_company_div.Name)
                .OpenRecord(.SQLCnSec, sSQLAdapter(5), sDataSet, "*", sec_user_company_role.Name)
                .OpenRecord(.SQLCnSec, sSQLAdapter(6), sDataSet, "*", sec_user_module.Name)
                .OpenRecord(.SQLCnSec, sSQLAdapter(7), sDataSet, "*", sec_user_report.Name)

                .SetIncrementSeed(sDataSet, sec_user_preference, "nID")
                .SetIncrementSeed(sDataSet, sec_user_favorite, "nID")
                .SetIncrementSeed(sDataSet, sec_user_company, "nID")
                .SetIncrementSeed(sDataSet, sec_user_company_div, "nID")
                .SetIncrementSeed(sDataSet, sec_user_company_role, "nID")
                .SetIncrementSeed(sDataSet, sec_user_module, "nID")
                .SetIncrementSeed(sDataSet, sec_user_report, "nID")

                .DatagridPopup(sec_user_preference, .SQLCnSec)
                .DatagridPopup(sec_user_favorite, .SQLCnSec)
                .DatagridPopup(sec_user_company, .SQLCnSec)
                .DatagridPopup(sec_user_company_div, .SQLCnSec)
                .DatagridPopup(sec_user_company_role, .SQLCnSec)
                .DatagridPopup(sec_user_module, .SQLCnSec)
                .DatagridPopup(sec_user_report, .SQLCnSec)

                bsChild(0).DataSource = sDataSet.Tables(sec_user_preference.Name)
                bsChild(1).DataSource = sDataSet.Tables(sec_user_favorite.Name)
                bsChild(2).DataSource = sDataSet.Tables(sec_user_company.Name)
                bsChild(3).DataSource = sDataSet.Tables(sec_user_company_div.Name)
                bsChild(4).DataSource = sDataSet.Tables(sec_user_company_role.Name)
                bsChild(5).DataSource = sDataSet.Tables(sec_user_module.Name)
                bsChild(6).DataSource = sDataSet.Tables(sec_user_report.Name)

                .DataGridBind(sec_user_preference, bsChild(0))
                .DataGridBind(sec_user_favorite, bsChild(1))
                .DataGridBind(sec_user_company, bsChild(2))
                .DataGridBind(sec_user_company_div, bsChild(3))
                .DataGridBind(sec_user_company_role, bsChild(4))
                .DataGridBind(sec_user_module, bsChild(5))
                .DataGridBind(sec_user_report, bsChild(6))

                GetLocalChild()

                LocalDatagridPop()
                LocalDatagridPop2()
                LocalDatagridPop3()
                LocalDatagridPop4()
                LocalDatagridPop5()
                LocalDatagridPop6()

                TBNew = True
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Function
    Public Function TBEdit(ByVal obj As Object) As Boolean
        obj.tag = "newedit"
        eAct = eAction.EditRec
        TBEdit = False
        With clsDBCon
            .ObjectControl(Me, ObjectMethod.ObjectLock, False)
            TBEdit = True
        End With
    End Function
    Public Function TBDelete(ByVal obj As Object) As Boolean
        With clsDBCon
            TBDelete = False
            If MsgBox("Are you sure you want to delete this item?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, TitleHeader) = MsgBoxResult.Yes Then
                .ExecuteNonQuery(.SQLCnSec, "UPDATE " & Me.Name & " SET lDelete=1 WHERE " & cUserName.Name & "='" & cUserName.Text & "'")

                obj.tag = "find"
                TBFind(obj)

                TBDelete = True

            End If
        End With
    End Function
    Public Function TBSave(ByVal obj As Object) As Boolean
        Try
            TBConnect()
            TBSave = False
            With clsDBCon
                .lKillRecursive = False
                cUserFullName.Text = cUserFName.Text & " " & cUserMName.Text & " " & cUserLName.Text
                If .MandatoryOK(Me) = False Then TBSave = False : Exit Function

                SaveFavoriteToTable()

                .ExecuteStoredProc(.SQLCnSec, "sp" & Me.Name, Me, eAct)
                Dim s As String = .GetSearchString(Me, True)
                nID.Text = .GetValueFrTable("nID", Me.Name, s)
                If eAct = eAction.EditRec Then
                    For Each row As DataRow In sDataSet.Tables(sec_user_preference.Name).Select("cUserName='" & cUserName.Text & "'", "cUserName")
                        row("cUserName") = cUserName.Text
                    Next

                    For Each row As DataRow In sDataSet.Tables(sec_user_favorite.Name).Select("cUserName='" & cUserName.Text & "'", "cUserName")
                        row("cUserName") = cUserName.Text
                    Next

                    For Each row As DataRow In sDataSet.Tables(sec_user_company.Name).Select("cUserName='" & cUserName.Text & "'", "cUserName")
                        row("cUserName") = cUserName.Text
                    Next

                    For Each row As DataRow In sDataSet.Tables(sec_user_company_div.Name).Select("nRefID=" & vCurrentCOID, "nRefID")
                        row("nRefID") = vCurrentCOID
                    Next

                    For Each row As DataRow In sDataSet.Tables(sec_user_company_role.Name).Select("nRefID=" & vCurrentCOID, "nRefID")
                        row("nRefID") = vCurrentCOID
                    Next

                    For Each row As DataRow In sDataSet.Tables(sec_user_module.Name).Select("nRefID=" & vCurrentCOID, "nRefID")
                        row("nRefID") = vCurrentCOID
                    Next

                    For Each row As DataRow In sDataSet.Tables(sec_user_report.Name).Select("nRefID=" & vCurrentCOID, "nRefID")
                        row("nRefID") = vCurrentCOID
                    Next

                Else
                    For Each row As DataRow In sDataSet.Tables(sec_user_preference.Name).Select("cUserName=''", "cUserName")
                        row("cUserName") = cUserName.Text
                    Next

                    For Each row As DataRow In sDataSet.Tables(sec_user_favorite.Name).Select("cUserName=''", "cUserName")
                        row("cUserName") = cUserName.Text
                    Next

                    For Each row As DataRow In sDataSet.Tables(sec_user_company.Name).Select("cUserName=''", "cUserName")
                        row("cUserName") = cUserName.Text
                    Next

                    For Each row As DataRow In sDataSet.Tables(sec_user_company_div.Name).Select("nRefID=0", "nRefID")
                        row("nRefID") = vCurrentCOID
                    Next

                    For Each row As DataRow In sDataSet.Tables(sec_user_company_role.Name).Select("nRefID=0", "nRefID")
                        row("nRefID") = vCurrentCOID
                    Next

                    For Each row As DataRow In sDataSet.Tables(sec_user_module.Name).Select("nRefID=0", "nRefID")
                        row("nRefID") = vCurrentCOID
                    Next

                    For Each row As DataRow In sDataSet.Tables(sec_user_report.Name).Select("nRefID=0", "nRefID")
                        row("nRefID") = vCurrentCOID
                    Next

                End If

                bsChild(0).EndEdit()
                bsChild(1).EndEdit()
                bsChild(2).EndEdit()
                bsChild(3).EndEdit()
                bsChild(4).EndEdit()
                bsChild(5).EndEdit()
                bsChild(6).EndEdit()


                sSQLAdapter(1).Update(sDataSet.Tables(sec_user_preference.Name))
                sSQLAdapter(2).Update(sDataSet.Tables(sec_user_favorite.Name))
                sSQLAdapter(3).Update(sDataSet.Tables(sec_user_company.Name))
                sSQLAdapter(4).Update(sDataSet.Tables(sec_user_company_div.Name))
                sSQLAdapter(5).Update(sDataSet.Tables(sec_user_company_role.Name))
                sSQLAdapter(6).Update(sDataSet.Tables(sec_user_module.Name))
                sSQLAdapter(7).Update(sDataSet.Tables(sec_user_report.Name))

                .ObjectControl(Me, ObjectMethod.ObjectLock, True)
                GetLocalChild()
                MsgBox(SaveMsg, MsgBoxStyle.Information, TitleHeader)

                TBSave = True

            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Function
    Public Function TBUndo(ByVal obj As Object) As Boolean
        TBUndo = False
        With clsDBCon
            obj.tag = "clear"
            TBFind(obj)

            TBUndo = True

        End With
    End Function
    Public Function TBFind(ByVal obj As Object) As Boolean
        Try
            TBFind = False
            With (clsDBCon)
                TBConnect()
                If obj.tag = "clear" Or obj.tag = "" Then
                    .WaitWindow(WaitLoadingMsg, True)
                    obj.tag = "find"
                    sDataSet.Tables.Clear()
                    .ObjectControl(Me, ObjectMethod.ObjectLock, True)

                    Dim sSearch As String
                    sSearch = .GetSearchString(Me)
                    .OpenRecord(.SQLCnSec, sSQLAdapter(0), sDataSet, "*", Me.Name, "lDelete=0 " & IIf(sSearch = "", "", " and " & sSearch))
                    .OpenRecord(.SQLCnSec, sSQLAdapter(1), sDataSet, "*", sec_user_preference.Name)
                    .OpenRecord(.SQLCnSec, sSQLAdapter(2), sDataSet, "*", sec_user_favorite.Name)
                    .OpenRecord(.SQLCnSec, sSQLAdapter(3), sDataSet, "*", sec_user_company.Name)
                    .OpenRecord(.SQLCnSec, sSQLAdapter(4), sDataSet, "*", sec_user_company_div.Name)
                    .OpenRecord(.SQLCnSec, sSQLAdapter(5), sDataSet, "*", sec_user_company_role.Name)
                    .OpenRecord(.SQLCnSec, sSQLAdapter(6), sDataSet, "*", sec_user_module.Name)
                    .OpenRecord(.SQLCnSec, sSQLAdapter(7), sDataSet, "*", sec_user_report.Name)

                    .SetIncrementSeed(sDataSet, sec_user_preference, "nID")
                    .SetIncrementSeed(sDataSet, sec_user_favorite, "nID")
                    .SetIncrementSeed(sDataSet, sec_user_company, "nID")
                    .SetIncrementSeed(sDataSet, sec_user_company_div, "nID")
                    .SetIncrementSeed(sDataSet, sec_user_company_role, "nID")
                    .SetIncrementSeed(sDataSet, sec_user_module, "nID")
                    .SetIncrementSeed(sDataSet, sec_user_report, "nID")

                    .DatagridPopup(dtgList, .SQLCnSec, Me.Name)
                    .DatagridPopup(sec_user_preference, .SQLCnSec)
                    .DatagridPopup(sec_user_favorite, .SQLCnSec)
                    .DatagridPopup(sec_user_company, .SQLCnSec)
                    .DatagridPopup(sec_user_company_div, .SQLCnSec)
                    .DatagridPopup(sec_user_company_role, .SQLCnSec)
                    .DatagridPopup(sec_user_module, .SQLCnSec)
                    .DatagridPopup(sec_user_report, .SQLCnSec)

                    sBindSource.DataSource = sDataSet.Tables(Me.Name)
                    .Databind(Me, sBindSource)

                    bsChild(0).DataSource = sDataSet.Tables(sec_user_preference.Name)
                    bsChild(1).DataSource = sDataSet.Tables(sec_user_favorite.Name)
                    bsChild(2).DataSource = sDataSet.Tables(sec_user_company.Name)
                    bsChild(3).DataSource = sDataSet.Tables(sec_user_company_div.Name)
                    bsChild(4).DataSource = sDataSet.Tables(sec_user_company_role.Name)
                    bsChild(5).DataSource = sDataSet.Tables(sec_user_module.Name)
                    bsChild(6).DataSource = sDataSet.Tables(sec_user_report.Name)

                    .DataGridBind(sec_user_preference, bsChild(0))
                    .DataGridBind(sec_user_favorite, bsChild(1))
                    .DataGridBind(sec_user_company, bsChild(2))
                    .DataGridBind(sec_user_company_div, bsChild(3))
                    .DataGridBind(sec_user_company_role, bsChild(4))
                    .DataGridBind(sec_user_module, bsChild(5))
                    .DataGridBind(sec_user_report, bsChild(6))

                    GetLocalChild()
                    sBindSource.Position = nBookMark

                    vCurrentCOID = 1
                    vCurrentRole = 1

                    LocalDatagridPop()
                    LocalDatagridPop2()
                    LocalDatagridPop3()
                    LocalDatagridPop4()
                    LocalDatagridPop5()
                    LocalDatagridPop6()
                    PopModule(0)
                    PopModule2(0)

                    .WaitWindow("", False)
                Else
                    obj.tag = "clear"
                    .ObjectControl(Me, ObjectMethod.ObjectLock, False)
                    .DataUnbind(Me)
                    sDataSet.Tables.Clear()
                    .ObjectControl(Me, ObjectMethod.Cleardata)

                End If
                TBFind = True
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Function
    Public Sub TBNewItem()
        Try
            Dim a As New DataGridViewRow
            Dim dgvCell1 As DataGridViewTextBoxCell
            Dim dgvCell2 As DataGridViewTextBoxCell
            Dim dgvCell3 As DataGridViewImageCell

            If dtgName = dtgFavorite.Name Then
                With dtgFavorite

                    Dim sResult As String = clsDBCon.PopupPicker(1, clsDBCon.SQLCnSet)
                    If sResult = "" Then Exit Sub
                    Dim s() As String = Split(Mid$(sResult, 2, Len(sResult) - 2), "▪")

                    dgvCell1 = New DataGridViewTextBoxCell
                    dgvCell2 = New DataGridViewTextBoxCell
                    dgvCell3 = New DataGridViewImageCell

                    .EditMode = DataGridViewEditMode.EditOnF2
                    Dim tmpDSet As New DataSet
                    clsDBCon.ExecuteQuery("SELECT nID,iIcon FROM sec_module WHERE cModuleID='" & s(0) & "'", tmpDSet)

                    dgvCell1.Value = tmpDSet.Tables(0).Rows(0).Item(0)
                    dgvCell2.Value = s(1)
                    dgvCell3.Value = clsDBCon.GetImage(tmpDSet.Tables(0).Rows(0).Item(1))
                    a.Cells.Add(dgvCell1)
                    a.Cells.Add(dgvCell2)
                    a.Cells.Add(dgvCell3)

                    .Rows.Add(a)

                    tmpDSet = Nothing

                End With
            ElseIf dtgName = dtgCompany.Name Then
                With dtgCompany
                    Dim sResult As String = clsDBCon.PopupPicker(12, clsDBCon.SQLCnSet)
                    If sResult = "" Then Exit Sub
                    Dim s() As String = Split(Mid$(sResult, 2, Len(sResult) - 2), "▪")

                    clsDBCon.SQLCn = clsDBCon.SQLCnSec
                    dgvCell1 = New DataGridViewTextBoxCell
                    dgvCell1.Value = clsDBCon.GetValueFrTable("nID", "sec_company", "cCompanyID='" & s(0) & "'")
                    a.Cells.Add(dgvCell1)

                    dgvCell1 = New DataGridViewTextBoxCell
                    dgvCell2 = New DataGridViewTextBoxCell

                    dgvCell1.Value = s(0)
                    dgvCell2.Value = s(1)

                    a.Cells.Add(dgvCell1)
                    a.Cells.Add(dgvCell2)

                    .Rows.Add(a)

                End With
            ElseIf dtgName = dtgDivision.Name Then
                With dtgDivision
                    Dim sResult As String = clsDBCon.PopupPicker(15, clsDBCon.SQLCnSet)
                    If sResult = "" Then Exit Sub
                    Dim s() As String = Split(Mid$(sResult, 2, Len(sResult) - 2), "▪")

                    clsDBCon.SQLCn = clsDBCon.SQLCnSec
                    dgvCell1 = New DataGridViewTextBoxCell
                    dgvCell1.Value = clsDBCon.GetValueFrTable("nID", "sec_division", "cDivID='" & s(0) & "'")
                    a.Cells.Add(dgvCell1)

                    dgvCell1 = New DataGridViewTextBoxCell
                    dgvCell2 = New DataGridViewTextBoxCell

                    dgvCell1.Value = s(0)
                    dgvCell2.Value = s(1)

                    a.Cells.Add(dgvCell1)
                    a.Cells.Add(dgvCell2)

                    .Rows.Add(a)

                End With
            ElseIf dtgName = dtgRole.Name Then
                With dtgRole
                    Dim sResult As String = clsDBCon.PopupPicker(16, clsDBCon.SQLCnSet)
                    If sResult = "" Then Exit Sub
                    Dim s() As String = Split(Mid$(sResult, 2, Len(sResult) - 2), "▪")

                    clsDBCon.SQLCn = clsDBCon.SQLCnSec
                    dgvCell1 = New DataGridViewTextBoxCell
                    dgvCell1.Value = clsDBCon.GetValueFrTable("nID", "sec_role", "cRoleID='" & s(0) & "'")
                    a.Cells.Add(dgvCell1)

                    dgvCell1 = New DataGridViewTextBoxCell
                    dgvCell2 = New DataGridViewTextBoxCell

                    dgvCell1.Value = s(0)
                    dgvCell2.Value = s(1)

                    a.Cells.Add(dgvCell1)
                    a.Cells.Add(dgvCell2)

                    .Rows.Add(a)

                End With
            ElseIf dtgName = dtgModule.Name Then
                With dtgModule
                    Dim sResult As String = clsDBCon.PopupPicker(1, clsDBCon.SQLCnSet)
                    If sResult = "" Then Exit Sub
                    Dim s() As String = Split(Mid$(sResult, 2, Len(sResult) - 2), "▪")

                    clsDBCon.SQLCn = clsDBCon.SQLCnSec
                    dgvCell1 = New DataGridViewTextBoxCell
                    dgvCell1.Value = clsDBCon.GetValueFrTable("nID", "sec_module", "cModuleID='" & s(0) & "'")
                    a.Cells.Add(dgvCell1)

                    dgvCell1 = New DataGridViewTextBoxCell
                    dgvCell2 = New DataGridViewTextBoxCell

                    dgvCell1.Value = s(0)
                    dgvCell2.Value = s(1)

                    a.Cells.Add(dgvCell1)
                    a.Cells.Add(dgvCell2)

                    .Rows.Add(a)

                End With

            ElseIf dtgName = dtgReport.Name Then
                With dtgReport
                    Dim sResult As String = clsDBCon.PopupPicker(17, clsDBCon.SQLCnSet)
                    If sResult = "" Then Exit Sub
                    Dim s() As String = Split(Mid$(sResult, 2, Len(sResult) - 2), "▪")

                    clsDBCon.SQLCn = clsDBCon.SQLCnSec
                    dgvCell1 = New DataGridViewTextBoxCell
                    dgvCell1.Value = clsDBCon.GetValueFrTable("nID", "sec_report", "cReportID='" & s(0) & "'")
                    a.Cells.Add(dgvCell1)

                    dgvCell1 = New DataGridViewTextBoxCell
                    dgvCell2 = New DataGridViewTextBoxCell

                    dgvCell1.Value = s(0)
                    dgvCell2.Value = s(1)

                    a.Cells.Add(dgvCell1)
                    a.Cells.Add(dgvCell2)

                    .Rows.Add(a)

                End With

            ElseIf dtgName = sec_user_preference.Name Then
                With sec_user_preference
                    .EditMode = DataGridViewEditMode.EditOnF2
                    Dim row As DataRow = sDataSet.Tables(.Name).NewRow
                    row("cUserName") = IIf(cUserName.Text = "", 0, cUserName.Text)
                    sDataSet.Tables(.Name).Rows.Add(row)
                End With
            ElseIf dtgName = sec_user_company.Name Then
                With sec_user_company
                    .EditMode = DataGridViewEditMode.EditOnF2
                    Dim row As DataRow = sDataSet.Tables(.Name).NewRow
                    row("cUserName") = IIf(cUserName.Text = "", 0, cUserName.Text)
                    sDataSet.Tables(.Name).Rows.Add(row)
                End With

            End If
            GetLocalChild()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Sub TBRemoveItem()
        Try
            If dtgName = dtgFavorite.Name Then
                dtgFavorite.Rows.Remove(dtgFavorite.CurrentRow)
            ElseIf dtgName = dtgCompany.Name Then
                dtgCompany.Rows.Remove(dtgCompany.CurrentRow)
            ElseIf dtgName = dtgDivision.Name Then
                dtgDivision.Rows.Remove(dtgDivision.CurrentRow)
            ElseIf dtgName = dtgRole.Name Then
                dtgRole.Rows.Remove(dtgRole.CurrentRow)
            ElseIf dtgName = dtgModule.Name Then
                dtgModule.Rows.Remove(dtgModule.CurrentRow)
            ElseIf dtgName = dtgReport.Name Then
                dtgReport.Rows.Remove(dtgReport.CurrentRow)

            ElseIf dtgName = sec_user_preference.Name Then
                bsChild(0).RemoveCurrent()
            ElseIf dtgName = sec_user_company.Name Then
                bsChild(2).RemoveCurrent()
            End If
            GetLocalChild()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Sub GetLocalChild()
        With clsDBCon
            .GetChild("cUserName='" & IIf(cUserName.Text = "", 0, cUserName.Text) & "'", bsChild(0))
            .GetChild("cUserName='" & IIf(cUserName.Text = "", 0, cUserName.Text) & "'", bsChild(1))
            .GetChild("cUserName='" & IIf(cUserName.Text = "", 0, cUserName.Text) & "'", bsChild(2))
            .GetChild("nRefID=" & IIf(vCurrentCOID = 0, 0, vCurrentCOID), bsChild(3))
            .GetChild("nRefID=" & IIf(vCurrentCOID = 0, 0, vCurrentCOID), bsChild(4))
            .GetChild("nRefID=" & IIf(vCurrentRole = 0, 0, vCurrentRole), bsChild(5))
            .GetChild("nRefID=" & IIf(vCurrentRole = 0, 0, vCurrentRole), bsChild(6))
        End With
        sDataSet.Tables(sec_user_preference.Name).DefaultView.RowFilter = "cUserName='" & IIf(cUserName.Text = "", 0, cUserName.Text) & "'"
        sDataSet.Tables(sec_user_favorite.Name).DefaultView.RowFilter = "cUserName='" & IIf(cUserName.Text = "", 0, cUserName.Text) & "'"
        sDataSet.Tables(sec_user_company.Name).DefaultView.RowFilter = "cUserName='" & IIf(cUserName.Text = "", 0, cUserName.Text) & "'"
        sDataSet.Tables(sec_user_company_div.Name).DefaultView.RowFilter = "nRefID=" & IIf(vCurrentCOID = 0, 0, vCurrentCOID)
        sDataSet.Tables(sec_user_company_role.Name).DefaultView.RowFilter = "nRefID=" & IIf(vCurrentCOID = 0, 0, vCurrentCOID)
        sDataSet.Tables(sec_user_module.Name).DefaultView.RowFilter = "nRefID=" & IIf(vCurrentRole = 0, 0, vCurrentRole)
        sDataSet.Tables(sec_user_report.Name).DefaultView.RowFilter = "nRefID=" & IIf(vCurrentRole = 0, 0, vCurrentRole)
    End Sub

    Private Sub bNavi_ItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles bNavi.ItemClicked
        Dim lRet As Boolean
        Select Case e.ClickedItem.Name
            Case "tsNew"
                lRet = TBNew(mode)
            Case "tsEdit"
                lRet = TBEdit(mode)
            Case "tsDelete"
                lRet = TBDelete(mode)
            Case "tsSave"
                lRet = TBSave(mode)
            Case "tsUndo"
                lRet = TBUndo(mode)
            Case "tsFind"
                lRet = TBFind(mode)
        End Select
        If lRet = True Then TBControl(e.ClickedItem)
    End Sub

    Public Sub TBControl(ByVal obj As Object)
        Select Case obj.name
            Case "tsNew", "tsEdit", "tsDelete", "tsSave", "tsUndo", "tsFind"
                tsNew.Enabled = False
                tsEdit.Enabled = False
                tsDelete.Enabled = False
                tsSave.Enabled = False
                tsUndo.Enabled = False
                tsFind.Enabled = False
                tsMoveFirst.Enabled = False
                tsMoveLast.Enabled = False
                tsMoveNext.Enabled = False
                tsMovePrev.Enabled = False
                tsCurrentPos.Enabled = False
        End Select

        Select Case obj.Name
            Case "tsNew"
                tsSave.Enabled = True
                tsUndo.Enabled = True
                mode.Tag = "newedit"
            Case "tsEdit"
                tsSave.Enabled = True
                tsUndo.Enabled = True
                mode.Tag = "newedit"
            Case "tsDelete"
                If mode.Tag = "find" Then
                    tsNew.Enabled = True
                    tsEdit.Enabled = True
                    tsDelete.Enabled = True
                    tsFind.Enabled = True
                    tsCurrentPos.Enabled = True
                    tsMoveFirst.Enabled = True
                    tsMoveLast.Enabled = True
                    tsMoveNext.Enabled = True
                    tsMovePrev.Enabled = True
                    mode.Tag = "find"
                Else
                    tsNew.Enabled = True
                    tsFind.Enabled = True
                    mode.Tag = "clear"
                End If
            Case "tsSave"
                tsNew.Enabled = True
                tsEdit.Enabled = True
                tsDelete.Enabled = True
                tsFind.Enabled = True
                tsCurrentPos.Enabled = True
                tsMoveFirst.Enabled = True
                tsMoveLast.Enabled = True
                tsMoveNext.Enabled = True
                tsMovePrev.Enabled = True
                mode.Tag = "find"
            Case "tsUndo"
                If mode.Tag = "new" Then
                    tsNew.Enabled = True
                    tsFind.Enabled = True
                    mode.Tag = "clear"
                Else
                    tsNew.Enabled = True
                    tsEdit.Enabled = True
                    tsDelete.Enabled = True
                    tsFind.Enabled = True
                    tsCurrentPos.Enabled = True
                    tsMoveFirst.Enabled = True
                    tsMoveLast.Enabled = True
                    tsMoveNext.Enabled = True
                    tsMovePrev.Enabled = True
                    mode.Tag = "find"
                End If
            Case "tsFind"
                If mode.Tag = "find" Then
                    tsNew.Enabled = True
                    tsEdit.Enabled = True
                    tsDelete.Enabled = True
                    tsFind.Enabled = True
                    tsCurrentPos.Enabled = True
                    tsMoveFirst.Enabled = True
                    tsMoveLast.Enabled = True
                    tsMoveNext.Enabled = True
                    tsMovePrev.Enabled = True
                    mode.Tag = "find"
                Else
                    tsNew.Enabled = True
                    tsFind.Enabled = True
                    mode.Tag = "clear"
                End If
        End Select
    End Sub

    Private Sub sBindSource_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles sBindSource.PositionChanged
        If mode.Tag = "find" Then GetLocalChild()
        nBookMark = sBindSource.Position
    End Sub

    Private Sub sec_user_preference_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sec_user_preference.MouseDown
        With sec_user_preference
            If .EditMode = DataGridViewEditMode.EditProgrammatically Then Exit Sub
            dtgName = .Name
            If e.Button = MouseButtons.Right Then
                mnuStrip.Show(sec_user_preference, New Drawing.Point(e.X, e.Y))
            End If
        End With
    End Sub

    Private Sub sec_user_favorite_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sec_user_favorite.MouseDown
        With sec_user_favorite
            If .EditMode = DataGridViewEditMode.EditProgrammatically Then Exit Sub
            dtgName = .Name
            If e.Button = MouseButtons.Right Then
                mnuStrip.Show(sec_user_favorite, New Drawing.Point(e.X, e.Y))
            End If
        End With
    End Sub

    Private Sub sec_user_company_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sec_user_company.MouseDown
        With sec_user_company
            If .EditMode = DataGridViewEditMode.EditProgrammatically Then Exit Sub
            dtgName = .Name
            If e.Button = MouseButtons.Right Then
                mnuStrip.Show(sec_user_company, New Drawing.Point(e.X, e.Y))
            End If
        End With
    End Sub

    Private Sub mnuStrip_ItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles mnuStrip.ItemClicked
        Select Case e.ClickedItem.Text
            Case "New Item"
                TBNewItem()
            Case "Remove Item"
                TBRemoveItem()
        End Select
    End Sub

    Private Sub cDeptPicker_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cDeptPicker.Click
        With clsDBCon
            TBConnect()
            cDeptID.Text = .PopupPicker(1, .SQLCnSet)
        End With
    End Sub

    Private Sub dtgFavorite_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dtgFavorite.MouseDown
        With dtgFavorite
            If .EditMode = DataGridViewEditMode.EditProgrammatically Then Exit Sub
            dtgName = .Name
            If e.Button = MouseButtons.Right Then
                mnuStrip.Show(dtgFavorite, New Drawing.Point(e.X, e.Y))
            End If
        End With
    End Sub

    Private Sub dtgRole_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgRole.Click
        vCurrentRole = dtgRole.CurrentRow.Cells(0).Value
    End Sub

    Private Sub dtgRole_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dtgRole.MouseDown
        With dtgRole
            If .EditMode = DataGridViewEditMode.EditProgrammatically Then Exit Sub
            dtgName = .Name
            If e.Button = MouseButtons.Right Then
                mnuStrip.Show(dtgRole, New Drawing.Point(e.X, e.Y))
            End If
        End With
    End Sub

    Private Sub dtgCompany_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgCompany.Click
        vCurrentCOID = dtgCompany.CurrentRow.Cells(0).Value
        LocalDatagridPop3()
    End Sub

    Private Sub dtgCompany_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dtgCompany.MouseDown
        With dtgCompany
            If .EditMode = DataGridViewEditMode.EditProgrammatically Then Exit Sub
            dtgName = .Name
            If e.Button = MouseButtons.Right Then
                mnuStrip.Show(dtgCompany, New Drawing.Point(e.X, e.Y))
            End If
        End With
    End Sub

    Private Sub dtgDivision_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dtgDivision.MouseDown
        With dtgDivision
            If .EditMode = DataGridViewEditMode.EditProgrammatically Then Exit Sub
            dtgName = .Name
            If e.Button = MouseButtons.Right Then
                mnuStrip.Show(dtgDivision, New Drawing.Point(e.X, e.Y))
            End If
        End With
    End Sub

    Private Sub dtgModule_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgModule.Click
        PopModule()
    End Sub

    Private Sub dtgModule_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dtgModule.MouseDown
        With dtgModule
            If .EditMode = DataGridViewEditMode.EditProgrammatically Then Exit Sub
            dtgName = .Name
            If e.Button = MouseButtons.Right Then
                mnuStrip.Show(dtgModule, New Drawing.Point(e.X, e.Y))
            End If
        End With
    End Sub

    Private Sub dtgReport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgReport.Click
        PopModule2()
    End Sub

    Private Sub dtgReport_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dtgReport.MouseDown
        With dtgReport
            If .EditMode = DataGridViewEditMode.EditProgrammatically Then Exit Sub
            dtgName = .Name
            If e.Button = MouseButtons.Right Then
                mnuStrip.Show(dtgReport, New Drawing.Point(e.X, e.Y))
            End If
        End With
    End Sub

    ''' <summary>
    ''' Favorites
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LocalDatagridPop()
        Try
            Dim tmpDSet As New DataSet
            Dim dtgX As DataGridViewRow
            Dim dgvCell As DataGridViewCell

            dtgFavorite.Rows.Clear()
            dtgFavorite.Columns.Clear()
            Dim colID As New System.Windows.Forms.DataGridViewTextBoxColumn
            Dim colModule As New System.Windows.Forms.DataGridViewTextBoxColumn
            Dim colIcon As New System.Windows.Forms.DataGridViewImageColumn
            colID.HeaderText = "ID"
            colID.Visible = False
            colModule.HeaderText = "Module Name"
            colModule.Width = 150
            colIcon.HeaderText = "Icon"
            dtgFavorite.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {colID, colModule, colIcon})

            Dim sCol As String = ""
            For i As Integer = 0 To bsChild(1).Count - 1
                sCol = sCol & sec_user_favorite.Rows(i).Cells(2).Value & ","
            Next
            sCol = Mid$(sCol, 1, Len(sCol) - 1)

            clsDBCon.ExecuteQuery("select nID,cModuleName,iIcon from sec_module where nID in (" & sCol & ")", tmpDSet)
            For i As Integer = 0 To tmpDSet.Tables(0).Rows.Count - 1
                dtgX = New DataGridViewRow

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("nID").ToString
                dtgX.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("cModuleName").ToString
                dtgX.Cells.Add(dgvCell)

                dgvCell = New DataGridViewImageCell
                dgvCell.Value = clsDBCon.GetImage(tmpDSet.Tables(0).Rows(i).Item("iIcon"))
                dtgX.Cells.Add(dgvCell)

                dtgFavorite.Rows.Add(dtgX)
            Next
            tmpDSet.Tables(0).Clear()
        Catch ex As Exception

        End Try


    End Sub

    ''' <summary>
    ''' Company
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LocalDatagridPop2()
        Try
            Dim tmpDSet As New DataSet
            Dim dtgX As DataGridViewRow
            Dim dgvCell As DataGridViewCell

            dtgCompany.Rows.Clear()
            dtgCompany.Columns.Clear()
            Dim colID As New System.Windows.Forms.DataGridViewTextBoxColumn
            Dim colCompanyID As New System.Windows.Forms.DataGridViewTextBoxColumn
            Dim colCompanyName As New System.Windows.Forms.DataGridViewTextBoxColumn

            colID.HeaderText = "ID"
            colID.Visible = False
            colCompanyID.HeaderText = "Company ID"
            colCompanyName.HeaderText = "Company Name"
            colCompanyName.Width = 200
            dtgCompany.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {colID, colCompanyID, colCompanyName})

            Dim sCol As String = ""
            For i As Integer = 0 To bsChild(2).Count - 1
                sCol = sCol & sec_user_company.Rows(i).Cells(1).Value & ","
            Next
            sCol = Mid$(sCol, 1, Len(sCol) - 1)

            clsDBCon.ExecuteQuery("select nID,cCompanyID,cCompanyName from sec_company where nID in (" & sCol & ")", tmpDSet)
            For i As Integer = 0 To tmpDSet.Tables(0).Rows.Count - 1
                dtgX = New DataGridViewRow

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("nID").ToString
                dtgX.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("cCompanyID").ToString
                dtgX.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("cCompanyName").ToString
                dtgX.Cells.Add(dgvCell)

                dtgCompany.Rows.Add(dtgX)
            Next
            tmpDSet.Tables(0).Clear()
        Catch ex As Exception

        End Try


    End Sub

    ''' <summary>
    ''' Division
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LocalDatagridPop3()
        Try
            Dim tmpDSet As New DataSet
            Dim dtgX As DataGridViewRow
            Dim dgvCell As DataGridViewCell

            dtgDivision.Rows.Clear()
            dtgDivision.Columns.Clear()
            Dim colID As New System.Windows.Forms.DataGridViewTextBoxColumn
            Dim colDivisionID As New System.Windows.Forms.DataGridViewTextBoxColumn
            Dim colDivisionName As New System.Windows.Forms.DataGridViewTextBoxColumn

            colID.HeaderText = "ID"
            colID.Visible = False
            colDivisionID.HeaderText = "Division ID"
            colDivisionName.HeaderText = "Division Name"
            colDivisionName.Width = 200
            dtgDivision.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {colID, colDivisionID, colDivisionName})

            Dim sCol As String = ""
            For i As Integer = 0 To bsChild(3).Count - 1
                sCol = sCol & sec_user_company_div.Rows(i).Cells(2).Value & ","
            Next
            sCol = Mid$(sCol, 1, Len(sCol) - 1)

            clsDBCon.ExecuteQuery("select nID,cDivID,cDivName from sec_division where nID in (" & sCol & ")", tmpDSet)
            For i As Integer = 0 To tmpDSet.Tables(0).Rows.Count - 1
                dtgX = New DataGridViewRow

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("nID").ToString
                dtgX.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("cDivID").ToString
                dtgX.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("cDivName").ToString
                dtgX.Cells.Add(dgvCell)

                dtgDivision.Rows.Add(dtgX)
            Next
            tmpDSet.Tables(0).Clear()
        Catch ex As Exception

        End Try


    End Sub

    ''' <summary>
    ''' Role
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LocalDatagridPop4()
        Try
            Dim tmpDSet As New DataSet
            Dim dtgX As DataGridViewRow
            Dim dgvCell As DataGridViewCell

            dtgRole.Rows.Clear()
            dtgRole.Columns.Clear()
            Dim colID As New System.Windows.Forms.DataGridViewTextBoxColumn
            Dim colRoleID As New System.Windows.Forms.DataGridViewTextBoxColumn
            Dim colRoleName As New System.Windows.Forms.DataGridViewTextBoxColumn

            colID.HeaderText = "ID"
            colID.Visible = False
            colRoleID.HeaderText = "Role ID"
            colRoleName.HeaderText = "Role Name"
            colRoleName.Width = 200
            dtgRole.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {colID, colRoleID, colRoleName})

            Dim sCol As String = ""
            For i As Integer = 0 To bsChild(4).Count - 1
                sCol = sCol & sec_user_company_role.Rows(i).Cells(2).Value & ","
            Next
            sCol = Mid$(sCol, 1, Len(sCol) - 1)

            clsDBCon.ExecuteQuery("select nID,cRoleID,cRoleName from sec_role where nID in (" & sCol & ")", tmpDSet)
            For i As Integer = 0 To tmpDSet.Tables(0).Rows.Count - 1
                dtgX = New DataGridViewRow

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("nID").ToString
                dtgX.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("cRoleID").ToString
                dtgX.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("cRoleName").ToString
                dtgX.Cells.Add(dgvCell)

                dtgRole.Rows.Add(dtgX)
            Next
            tmpDSet.Tables(0).Clear()
        Catch ex As Exception

        End Try


    End Sub

    ''' <summary>
    ''' Module
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LocalDatagridPop5()
        Try
            Dim tmpDSet As New DataSet
            Dim dtgX As DataGridViewRow
            Dim dgvCell As DataGridViewCell

            dtgModule.Rows.Clear()
            dtgModule.Columns.Clear()
            Dim colID As New System.Windows.Forms.DataGridViewTextBoxColumn
            Dim colModuleID As New System.Windows.Forms.DataGridViewTextBoxColumn
            Dim colModuleName As New System.Windows.Forms.DataGridViewTextBoxColumn
            Dim colModuleAccess As New System.Windows.Forms.DataGridViewTextBoxColumn

            colID.HeaderText = "ID"
            colID.Visible = False
            colModuleID.HeaderText = "Module ID"
            colModuleName.HeaderText = "Module Name"
            colModuleName.Width = 200
            colModuleAccess.HeaderText = "Module Access"
            colModuleAccess.Visible = False
            dtgModule.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {colID, colModuleID, colModuleName, colModuleAccess})

            Dim sCol As String = ""
            For i As Integer = 0 To bsChild(5).Count - 1
                sCol = sCol & sec_user_module.Rows(i).Cells(2).Value & ","
            Next
            sCol = Mid$(sCol, 1, Len(sCol) - 1)

            clsDBCon.ExecuteQuery("select nID,cModuleID,cModuleName from sec_module where nID in (" & sCol & ")", tmpDSet)
            For i As Integer = 0 To tmpDSet.Tables(0).Rows.Count - 1
                dtgX = New DataGridViewRow

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("nID").ToString
                dtgX.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("cModuleID").ToString
                dtgX.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("cModuleName").ToString
                dtgX.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = IIf(sec_user_module.Rows.Count <> 0, sec_user_module.Rows(i).Cells(3).Value, "")
                dtgX.Cells.Add(dgvCell)

                dtgModule.Rows.Add(dtgX)
            Next
            tmpDSet.Tables(0).Clear()
        Catch ex As Exception

        End Try


    End Sub

    ''' <summary>
    ''' Report
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LocalDatagridPop6()
        Try
            Dim tmpDSet As New DataSet
            Dim dtgX As DataGridViewRow
            Dim dgvCell As DataGridViewCell

            dtgReport.Rows.Clear()
            dtgReport.Columns.Clear()
            Dim colID As New System.Windows.Forms.DataGridViewTextBoxColumn
            Dim colReportID As New System.Windows.Forms.DataGridViewTextBoxColumn
            Dim colReportName As New System.Windows.Forms.DataGridViewTextBoxColumn
            Dim colReportAccess As New System.Windows.Forms.DataGridViewTextBoxColumn

            colID.HeaderText = "ID"
            colID.Visible = False
            colReportID.HeaderText = "Report ID"
            colReportName.HeaderText = "Report Name"
            colReportName.Width = 200
            colReportAccess.HeaderText = "Report Access"
            colReportAccess.Visible = False
            dtgReport.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {colID, colReportID, colReportName, colReportAccess})

            Dim sCol As String = ""
            For i As Integer = 0 To bsChild(6).Count - 1
                sCol = sCol & sec_user_report.Rows(i).Cells(2).Value & ","
            Next
            sCol = Mid$(sCol, 1, Len(sCol) - 1)

            clsDBCon.ExecuteQuery("select nID,cReportID,cReportName from sec_report where nID in (" & sCol & ")", tmpDSet)
            For i As Integer = 0 To tmpDSet.Tables(0).Rows.Count - 1
                dtgX = New DataGridViewRow

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("nID").ToString
                dtgX.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("cReportID").ToString
                dtgX.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("cReportName").ToString
                dtgX.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = IIf(sec_user_report.Rows.Count <> 0, sec_user_report.Rows(i).Cells(3).Value, "")
                dtgX.Cells.Add(dgvCell)

                dtgReport.Rows.Add(dtgX)
            Next
            tmpDSet.Tables(0).Clear()
        Catch ex As Exception

        End Try


    End Sub

    ''' <summary>
    ''' Popup Module
    ''' </summary>
    ''' <param name="nRowNumber"></param>
    ''' <remarks></remarks>
    Public Sub PopModule(Optional ByVal nRowNumber As Integer = -1)
        Dim tmpDSet As New DataSet
        Dim dtgX As DataGridViewRow
        Dim dgvCell As DataGridViewCell
        Dim lAccess As String = ""

        Dim sCurrentRow As String
        Try
            If dtgModule.Rows.Count > 0 Then
                If nRowNumber <> -1 Then
                    sCurrentRow = dtgModule.Rows(0).Cells(0).Value
                Else
                    sCurrentRow = dtgModule.CurrentRow.Cells(0).Value
                End If

                For i As Integer = 0 To dtgModule.Rows.Count - 1
                    If sCurrentRow = dtgModule.Rows(i).Cells(0).Value Then
                        lAccess = dtgModule.Rows(i).Cells(3).Value
                        vModBookMark = i
                        Exit For
                    End If
                Next
            End If
            With clsDBCon
                dtgAccess.Rows.Clear()
                dtgAccess.Columns.Clear()

                Dim colFunction As New System.Windows.Forms.DataGridViewTextBoxColumn
                Dim colAccess As New System.Windows.Forms.DataGridViewCheckBoxColumn
                colFunction.HeaderText = "Function"
                colFunction.Width = 100
                colAccess.HeaderText = "Access"
                colAccess.Width = 100
                dtgAccess.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {colFunction, colAccess})

                If dtgModule.Rows.Count > 0 Then
                    .ExecuteQuery("select cDesc from sec_toolbarfunctionality", tmpDSet)
                    For i As Integer = 0 To tmpDSet.Tables(0).Rows.Count - 1
                        dtgX = New DataGridViewRow

                        dgvCell = New DataGridViewTextBoxCell()
                        dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("cDesc").ToString
                        dtgX.Cells.Add(dgvCell)

                        dgvCell = New DataGridViewCheckBoxCell
                        dgvCell.Value = IIf(Mid$(lAccess, i + 1, 1) = "1", True, False) 'False
                        dtgX.Cells.Add(dgvCell)

                        dtgAccess.Rows.Add(dtgX)
                    Next
                    tmpDSet.Tables(0).Clear()
                End If
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Popup Report
    ''' </summary>
    ''' <param name="nRowNumber"></param>
    ''' <remarks></remarks>
    Public Sub PopModule2(Optional ByVal nRowNumber As Integer = -1)
        Dim tmpDSet As New DataSet
        Dim dtgX As DataGridViewRow
        Dim dgvCell As DataGridViewCell
        Dim lAccess As String = ""

        Dim sCurrentRow As String
        Try
            If dtgReport.Rows.Count > 0 Then
                If nRowNumber <> -1 Then
                    sCurrentRow = dtgReport.Rows(0).Cells(0).Value
                Else
                    sCurrentRow = dtgReport.CurrentRow.Cells(0).Value
                End If

                For i As Integer = 0 To dtgReport.Rows.Count - 1
                    If sCurrentRow = dtgReport.Rows(i).Cells(0).Value Then
                        lAccess = dtgReport.Rows(i).Cells(3).Value
                        vModBookMark = i
                        Exit For
                    End If
                Next
            End If

            With clsDBCon
                dtgReportAccess.Rows.Clear()
                dtgReportAccess.Columns.Clear()

                Dim colFunction As New System.Windows.Forms.DataGridViewTextBoxColumn
                Dim colAccess As New System.Windows.Forms.DataGridViewCheckBoxColumn
                colFunction.HeaderText = "Function"
                colFunction.Width = 100
                colAccess.HeaderText = "Access"
                colAccess.Width = 100
                dtgReportAccess.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {colFunction, colAccess})

                If dtgReport.Rows.Count > 0 Then
                    .ExecuteQuery("select cDesc from sec_toolbarfunctionality", tmpDSet)
                    For i As Integer = 0 To tmpDSet.Tables(0).Rows.Count - 1
                        dtgX = New DataGridViewRow

                        dgvCell = New DataGridViewTextBoxCell()
                        dgvCell.Value = tmpDSet.Tables(0).Rows(i).Item("cDesc").ToString
                        dtgX.Cells.Add(dgvCell)

                        dgvCell = New DataGridViewCheckBoxCell
                        dgvCell.Value = IIf(Mid$(lAccess, i + 1, 1) = "1", True, False) 'False
                        dtgX.Cells.Add(dgvCell)

                        dtgReportAccess.Rows.Add(dtgX)
                    Next
                    tmpDSet.Tables(0).Clear()
                End If
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub SaveFavoriteToTable()
        Try
            For i As Integer = 0 To bsChild(1).Count - 1
                bsChild(1).RemoveCurrent()
            Next
            For i As Integer = 0 To dtgFavorite.Rows.Count - 1
                Dim row As DataRow = sDataSet.Tables(sec_user_favorite.Name).NewRow
                row("cModuleID") = dtgFavorite.Rows(i).Cells(0).Value
                row("cUserName") = cUserName.Text
                sDataSet.Tables(sec_user_favorite.Name).Rows.Add(row)
            Next

            For i As Integer = 0 To bsChild(2).Count - 1
                bsChild(2).RemoveCurrent()
            Next
            For i As Integer = 0 To dtgCompany.Rows.Count - 1
                Dim row As DataRow = sDataSet.Tables(sec_user_company.Name).NewRow
                row("cCompanyID") = dtgCompany.Rows(i).Cells(0).Value
                row("cUserName") = cUserName.Text
                sDataSet.Tables(sec_user_company.Name).Rows.Add(row)
            Next

            For i As Integer = 0 To bsChild(3).Count - 1
                bsChild(3).RemoveCurrent()
            Next
            For i As Integer = 0 To dtgDivision.Rows.Count - 1
                Dim row As DataRow = sDataSet.Tables(sec_user_company_div.Name).NewRow
                row("nRefID") = vCurrentCOID
                row("cDivID") = dtgDivision.Rows(i).Cells(0).Value
                sDataSet.Tables(sec_user_company_div.Name).Rows.Add(row)
            Next

            For i As Integer = 0 To bsChild(4).Count - 1
                bsChild(4).RemoveCurrent()
            Next
            For i As Integer = 0 To dtgRole.Rows.Count - 1
                Dim row As DataRow = sDataSet.Tables(sec_user_company_role.Name).NewRow
                row("nRefID") = vCurrentCOID
                row("cRoleRefID") = dtgRole.Rows(i).Cells(0).Value
                sDataSet.Tables(sec_user_company_role.Name).Rows.Add(row)
            Next


            For i As Integer = 0 To bsChild(5).Count - 1
                bsChild(5).RemoveCurrent()
            Next
            For i As Integer = 0 To dtgModule.Rows.Count - 1
                Dim row As DataRow = sDataSet.Tables(sec_user_module.Name).NewRow
                row("nRefID") = vCurrentCOID
                row("cModuleID") = dtgModule.Rows(i).Cells(0).Value
                row("lTBAccess") = dtgModule.Rows(i).Cells(3).Value
                sDataSet.Tables(sec_user_module.Name).Rows.Add(row)
            Next

            For i As Integer = 0 To bsChild(6).Count - 1
                bsChild(6).RemoveCurrent()
            Next
            For i As Integer = 0 To dtgReport.Rows.Count - 1
                Dim row As DataRow = sDataSet.Tables(sec_user_report.Name).NewRow
                row("nRefID") = vCurrentCOID
                row("cReportID") = dtgReport.Rows(i).Cells(0).Value
                row("lTBAccess") = dtgReport.Rows(i).Cells(3).Value
                sDataSet.Tables(sec_user_report.Name).Rows.Add(row)
            Next


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
        Dim s As String = ""
        For i As Integer = 0 To dtgAccess.Rows.Count - 1
            s = s & IIf(dtgAccess.Rows(i).Cells(1).Value = True, 1, 0)
        Next
        dtgModule.Rows(vModBookMark).Cells(3).Value = s
    End Sub

    Private Sub btnReportApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReportApply.Click
        Dim s As String = ""
        For i As Integer = 0 To dtgReportAccess.Rows.Count - 1
            s = s & IIf(dtgReportAccess.Rows(i).Cells(1).Value = True, 1, 0)
        Next
        dtgReport.Rows(vModBookMark).Cells(3).Value = s
    End Sub

End Class
